package com.healthcarex.hip.saas.message.factory;


import com.healthcarex.hip.saas.message.processor.IMessageProcessor;
import com.healthcarex.hip.saas.message.service.MessageService;
import com.healthcarex.hip.saas.message.core.AbstractMessage;
import com.healthcarex.hip.saas.message.core.MessageModel;
import com.healthcarex.hip.saas.message.creator.IMessageCreator;
import com.healthcarex.hip.saas.message.schema.MessageSchemaDefinition;

import javax.xml.parsers.ParserConfigurationException;


public class MessageFactory {

    /**
     * 解析消息
     *
     * @param messageType
     * @param message
     * @param msd         msd
     */
    public static MessageModel parseMessage(String messageType,
                                            Object message, MessageSchemaDefinition msd) throws Exception {
        MessageService service = MessageService.getInstance();
        IMessageCreator creator = service.getCreator(messageType);
        IMessageProcessor processor = service.getProcessor(messageType);

        if (creator != null && processor != null) {
            AbstractMessage inMessage = creator.create(message, msd);
            return processor.parse(inMessage);
        }

        return null;
    }

    /**
     * 生成消息
     *
     * @param messageType
     * @param message
     * @param msd         msd
     */
    public static AbstractMessage generateMessage(String messageType,
                                                  MessageModel message, MessageSchemaDefinition msd) {
        if (message != null && msd != null) {
            MessageService service = MessageService.getInstance();
            IMessageProcessor processor = service.getProcessor(messageType);

            if (processor != null) {
                return processor.generate(message, msd);
            }
        }

        return null;
    }
}